const app = getApp();
import Notify from '@vant/weapp/notify/notify';
Page({

  data: {
    industry: {},
    gangweifenlei: {},
    workList: [{
      name: "",
      industry: "",
      post: "",
      time: ['', '']
    }],
    role_id: 3
  },

  onLoad: function () {
    this.getIndustry()
    this.getGangweifenlei()
  },

  onShow: function () {
  },

  save() {
    let workList = {
      work_list: this.data.workList
    }
    let post_data = {
      "role_id": this.data.role_id,
      "form": JSON.stringify(workList)
    }
    var that = this;
    wx.request({
      url: app.globalData.domain + 'interviewer/new_save/?role',
      method: "POST",
      data: post_data,
      header: {
        "Token": app.getToken(),
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      success: function (res) {
        if (res.data.response == "ok") {
          Notify({
            type: 'success',
            message: "保存成功"
          });
        } else {
          Notify({
            type: 'danger',
            message: res.data.message
          });
        }
      }
    })
  },

  onBeginPickerChange(e) {
    this.setData({
      [`workList[${e.currentTarget.dataset.info}].time[0]`]: this.formatDate(e.detail.value)
    })
  },
  onEndPickerChange(e) {
    this.setData({
      [`workList[${e.currentTarget.dataset.info}].time[1]`]: this.formatDate(e.detail.value)
    })
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },

  onNameChange(e){
    this.setData({
      [`workList[${e.currentTarget.dataset.info}].name`]: e.detail
    })
  },

  onIndustryChange(e){
    this.setData({
      [`workList[${e.currentTarget.dataset.info}].industry`]: e.detail
    })
  },

  onPostChange(e){1
    this.setData({
      [`workList[${e.currentTarget.dataset.info}].post`]: e.detail
    })
  },

  addWorkList() {
    let work = {
      name: "",
      industry: "",
      post: "",
      time: ['', '']
    }
    this.data.workList.push(work)
    this.setData({workList: this.data.workList})
  },
  getIndustry() {
    let that = this
    wx.request({
      url: app.globalData.domain + 'tea/industry/',
      method: "GET",
      header: {
        "Token": app.getToken(),
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      success: function (res) {
        if (res.data.response == "ok") {
          that.setData({industry: res.data.data})
        } else {
          Notify({
            type: 'danger',
            message: res.data.message
          });
        }
      }
    })
  },
  getGangweifenlei() {
    let that = this
    wx.request({
      url: app.globalData.domain + 'tea/gangweifenlei/',
      method: "GET",
      header: {
        "Token": app.getToken(),
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      success: function (res) {
        if (res.data.response == "ok") {
          that.setData({gangweifenlei: res.data.data})
        } else {
          Notify({
            type: 'danger',
            message: res.data.message
          });
        }
      }
    })
  }

})